import {createRouter, createWebHashHistory, RouteRecordRaw,} from "vue-router";
import {defineAsyncComponent} from "vue";

const Layout = defineAsyncComponent(() => import('../views/layout/Layout.vue'))
const routes: Array<RouteRecordRaw> = [
    // 登录
    {
        path: '/login',
        component: defineAsyncComponent(() => import('../views/login/Login.vue'))
    },
    // 后台首页
    {
        path: '/',
        component: Layout,
        meta: {title: '后台首页', path: '/home'},
        redirect: '/home',
        children: [
            {
                path: '/home',
                component: defineAsyncComponent(() => import('../views/home/Home.vue'))
            }
        ]
    },
    /* 订单管理*/
    {
        path: '/order',
        component: Layout,
        meta: {title: '订单管理', path: '/order'},
        children: [
            {
                path: '',
                component: defineAsyncComponent(() => import('../views/order/Order.vue'))
            },
            {
                path: '/order/order-edit',
                component: defineAsyncComponent(() => import('../views/order/OrderEdit.vue'))
            }
        ]
    },
    /* 商品管理*/
    {
        path: '/goods',
        component: Layout,
        meta: {title: '商品管理', path: '/goods'},
        redirect: '/goods/goods-list',
        children: [
            {
                meta: {title: '商品列表', path: '/goods/goods-list'},
                path: '/goods/goods-list',
                component: defineAsyncComponent(() => import('../views/goods/GoodsList.vue')),
            },
            {
                meta: {title: '商品添加', path: '/goods/goods-add'},
                path: '/goods/goods-add',
                component: defineAsyncComponent(() => import('../views/goods/GoodsAdd.vue')),
            },
            {
                meta: {title: '商品分类', path: '/goods/goods-cate'},
                path: '/goods/goods-cate',
                component: defineAsyncComponent(() => import('../views/goods/GoodsCate.vue')),
            },
            {
                path: '/goods/goods-edit',
                component: defineAsyncComponent(() => import('../views/goods/GoodsEdit.vue')),
            },
        ],
    },
    /* 店铺管理*/
    {
        path: '/shop',
        component: Layout,
        meta: {title: '店铺管理', path: 'shop'},
        children: [
            {
                path: '',
                component: defineAsyncComponent(() => import('../views/shop/Shop.vue'))
            }
        ]
    },
    /*帐号管理*/
    {
        path: '/account',
        meta: {title: '账号管理', path: '/account'},
        component: Layout,
        redirect: '/account/account-list',
        children: [
            {
                meta: {title: '账号列表', path: './account/account-list'},
                path: '/account/account-list',
                component: defineAsyncComponent(() => import('../views/account/AccountList.vue')),
            },
            {
                meta: {title: '账号添加', path: '/account/account-add'},
                path: '/account/account-add',
                component: defineAsyncComponent(() => import('../views/account/AccountAdd.vue')),
            },
            {
                meta: {title: '修改密码', path: '/account/password-modify'},
                path: '/account/password-modify',
                component: defineAsyncComponent(() => import('../views/account/PasswordModify.vue')),
            },
            {
                meta: {title: '个人中心', path: '/account/person'},
                path: '/account/person',
                component: defineAsyncComponent(() => import('../views/account/Person.vue')),
            },
        ]
    },
    /*销售统计*/
    {
        path: '/total',
        component: Layout,
        meta: {title: '销售统计', path: '/total'},
        redirect: '/total/total-goods',
        children: [
            {
                path: '/total/total-goods',
                meta: {title: '商品统计', path: '/total/total-goods'},
                component: defineAsyncComponent(() => import('../views/total/TotalGoods.vue')),
            },
            {
                path: '/total/total-order',
                meta: {title: '订单统计', path: '/total/total-order'},
                component: defineAsyncComponent(() => import('../views/total/TotalOrder.vue')),
            },
        ],
    },
    {
        path: '/components-list',
        name: 'LIST',
        component: defineAsyncComponent(() => import('../components/List.vue'))
    },
    {
        path: '/yayaya',
        name: 'YAYAYA',
        component: defineAsyncComponent(() => import('../components/HelloWorld.vue'))
    }
]
const router = createRouter({
    history: createWebHashHistory(),
    routes
})
router.beforeEach((to, from, next) => {
    if (to.path === '/login') return next()
    const token = window.sessionStorage.getItem('t_k')
    if (!token || token == 'undefined') return next('/login')
    if (token == null) return next('/login')
    next()
})
export default router

